<?php


namespace app\admin\controller;


use app\BaseController;
use app\Request;

class Car extends  BaseController
{
    public function getList(Request  $request)
    {
        $page = $request->post('page', 1);
        $limit = $request->post('limit', 10);
        $where = [];
        if(!empty($this->userInfo['username']) && $this->userInfo['username'] !='admin'){
            $where[] = ['manager_id','=',$this->userInfo['id']];
        }
        $license_plate = $request->post('license_plate', '');
        if(!empty($license_plate)){
            $where[] = ['license_plate','like',"%{$license_plate}%"];
        }
        $store_id = $request->post('store_id',0);
        if(!empty($store_id)){
            $where[] = ['store_id','=',$store_id];
        }
        $manager_id = $request->post('manager_id',0);
        if(!empty($manager_id)){
            $where[] = ['manager_id','=',$manager_id];
        }
        $model = \app\admin\model\Car::order('id desc')->where($where)->append(['license_type'])->with([
            'user' => function($query) {
                $query->field('id,wxname,wximage');
            }
        ]);

        $count = \app\admin\model\Car::where($where)->count('id');
        $list = $model->page($page, $limit)->select();
        //增加商户信息
        $list = \app\admin\model\Manager::getMananger($list);
        $data = [
            'total'   => $count,
            'current' => $page,
            'perage'  => $limit,
            'data'    => $list
        ];

        return show(config('status.success'), 'success', $data);
    }
}